package com.rgs.module.login.util;

import com.rgs.base.core.system.WebResponseState;
import com.rgs.module.login.bo.LoginUserBO;
import com.rgs.module.login.exception.LoginException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


/**
 * @author Ren Gaoshuai
 * @ClassName SecurityUtil
 * @date 2021-12-24 17:12
 **/
public class SecurityUtil {
    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        try {
            return getLoginUser().getUsername();
        } catch (Exception e) {
            throw new LoginException(WebResponseState.UNAUTHORIZED);
        }
    }


    /**
     * 获取用户
     **/
    public static LoginUserBO getLoginUser() {
        try {
            LoginUserBO loginUser = (LoginUserBO) getAuthentication().getPrincipal();
            return loginUser;
        } catch (Exception e) {
            throw new LoginException(WebResponseState.UNAUTHORIZED);
        }
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {

        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 密码强度检测
     *
     * @return 密码强度
     * Z = 字母 S = 数字 T = 特殊字符
     * 一、假定密码字符数范围6-16，除英文数字和字母外的字符都视为特殊字符：
     * 弱：^[0-9A-Za-z]{6,16}$
     * 中：^(?=.{6,16})[0-9A-Za-z]*[^0-9A-Za-z][0-9A-Za-z]*$
     * 强：^(?=.{6,16})([0-9A-Za-z]*[^0-9A-Za-z][0-9A-Za-z]*){2,}$
     * 二、假定密码字符数范围6-16，密码字符允许范围为ASCII码表字符：
     * 弱：^[0-9A-Za-z]{6,16}$
     * 中：^(?=.{6,16})[0-9A-Za-z]*[\x00-\x2f\x3A-\x40\x5B-\xFF][0-9A-Za-z]*$
     * 强：^(?=.{6,16})([0-9A-Za-z]*[\x00-\x2F\x3A-\x40\x5B-\xFF][0-9A-Za-z]*){2,}$
     */
    public static String checkPasswordLevel(String passwordStr) {
        String regexZ = "\\d*";
        String regexS = "[a-zA-Z]+";
        String regexT = "\\W+$";
        String regexZt = "\\D*";
        String regexSt = "[\\d\\W]*";
        String regexZs = "\\w*";
        String regexZst = "[\\w\\W]*";

        if (passwordStr.matches(regexZ)) {
            return "弱";
        }
        if (passwordStr.matches(regexS)) {
            return "弱";
        }
        if (passwordStr.matches(regexT)) {
            return "弱";
        }
        if (passwordStr.matches(regexZt)) {
            return "中";
        }
        if (passwordStr.matches(regexSt)) {
            return "中";
        }
        if (passwordStr.matches(regexZs)) {
            return "中";
        }
        if (passwordStr.matches(regexZst)) {
            return "强";
        }
        return passwordStr;
    }

}
